import 'package:flutter/material.dart';

import '../models/member_address_model.dart';
import '../tbicons.dart';
import '../utils/logger.dart';
import '../widgets/list_state_page.dart';
import '../widgets/tags/btn_text.dart';
import '../widgets/tags/empty.dart';

import '../application.dart';

class AddressPicker extends StatefulWidget {
  final int addressId;

  const AddressPicker({Key? key, this.addressId = 0}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _AddressPickerState();
}

class _AddressPickerState
    extends ListStatePage<AddressPicker, MemberAddressModel> {
  int addressId = 0;

  @override
  void initState() {
    super.initState();
    addressId = widget.addressId;
  }

  @override
  didChangeDependencies() {
    super.didChangeDependencies();
    logger.info('didChangeDependencies');
  }

  MemberAddressModel getAddress() {
    if (addressId == 0) {
      return MemberAddressModel();
    }
    return lists.firstWhere((element) => element.id == addressId);
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;
    return Column(
      children: [
        const SizedBox(height: 20),
        Expanded(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 15),
            child: buildListView(),
          ),
        ),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Expanded(
                child: OutlinedButton(
                  onPressed: () {
                    editAddress(0);
                  },
                  child: const BtnText('添加地址', vPadding: BtnText.vpNormal),
                ),
              ),
              const SizedBox(width: 10),
              Expanded(
                child: ElevatedButton(
                  onPressed: () {
                    Navigator.pop(context, getAddress());
                  },
                  child: const BtnText('确定', vPadding: BtnText.vpNormal),
                ),
              ),
            ],
          ),
        )
      ],
    );
  }

  @override
  Widget renderEmpty() {
    return const TagEmpty('还没有添加地址哦~', icon: Tbicons.location_light);
  }

  @override
  String get apiUrl => 'member.address/index';

  editAddress(int id) async {
    await app.route('/member/address/detail', arguments: {'id': id});
    reloadData();
  }

  bool itemChecked(int id) {
    return id == addressId;
  }

  @override
  Widget buildItem(MemberAddressModel item) {
    final colorScheme = Theme.of(context).colorScheme;
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          color: itemChecked(item.id)
              ? colorScheme.primary.withAlpha(150)
              : Colors.grey[200]!,
        ),
        borderRadius: const BorderRadius.all(Radius.circular(5)),
      ),
      padding: const EdgeInsets.all(10),
      child: InkWell(
        onTap: () {
          setState(() {
            addressId = addressId == item.id ? 0 : item.id;
          });
        },
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Icon(
              itemChecked(item.id)
                  ? Tbicons.roundcheckfill
                  : Tbicons.roundcheck,
              color: itemChecked(item.id)
                  ? colorScheme.primary
                  : Colors.grey[200]!,
            ),
            const SizedBox(width: 10),
            Expanded(
              child: SizedBox(
                height: 60,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Text("${item.receiveName}/${item.mobile}"),
                    Text(item.areaView),
                    Text(item.address),
                  ],
                ),
              ),
            ),
            GestureDetector(
                onTap: () {
                  editAddress(item.id);
                },
                child: const Icon(Tbicons.edit_light)),
          ],
        ),
      ),
    );
  }

  @override
  MemberAddressModel transItem(item) => MemberAddressModel.fromJson(item);
}
